package activity;

import activity.marketing.ActivityClassify;
import enums.ActivityType;
import enums.ShowPosition;
import helper.CacheHelper;
import models.BaseModel;
import models.constants.DeletedStatus;
import play.db.jpa.JPA;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by shancheng on 16-6-27.
 */
@Entity
@Table(name = "activity")
public class Activity extends BaseModel {

    public static final String CACHE_ACTIVITY_BY_TEMPID = "ULC_CACHE_ACTIVITY_BY_TEMPID_";
    public static final String CACHE_ACTIVITY_ID = "ULC_CACHE_ACTIVITY_";
    public static final String CACHE_ACTIVITY_PRODUCT_ACTIVITYID = "ULC_CACHE_ACTIVITY_PRODUCT_ACTIVITYID_";

    /**
     * 活动标题
     */
    @Column(name = "title")
    public String title;


    /**
     * 副标题
     */
    @Column(name = "subtitle")
    public String subtitle;

    /**
     * 活动简介
     */
    @Column(name = "content")
    public String content;

    @ManyToOne
    @JoinColumn(name = "activity_home_template_id")
    public ActivityHomeTemplate activityHomeTemplate;

    /**
     * 分类活动
     */
    @ManyToOne
    @JoinColumn(name = "activity_classify_id")
    public ActivityClassify activityClassify;


    /**
     * 活动主图
     */
    @Column(name = "main_img")
    public String mainImg;


    /**
     * 活动类型
     */
    @Column(name = "type")
    @Enumerated(EnumType.STRING)
    public ActivityType type;

    /**
     * 活动类型(所在位置)
     */
    @Column(name = "show_position")
    @Enumerated(EnumType.STRING)
    public ShowPosition showPosition;

    /**
     * 排序号
     */
    @Column(name = "show_order")
    public Long showOrder;

    /**
     * 活动链接
     */
    @Column(name = "url")
    public String url;

    /**
     * 开始时间
     */
    @Column(name = "begin_at")
    @Temporal(TemporalType.TIMESTAMP)
    public Date beginAt;


    /**
     * 结束时间
     */
    @Column(name = "end_at")
    @Temporal(TemporalType.TIMESTAMP)
    public Date endAt;

    /**
     * 开始时间
     */
    @Column(name = "begin_times")
    public String beginTimes;


    /**
     * 活动结束时间
     */
    @Column(name = "end_times")
    public String endTimes;


    /**
     * 逻辑删除状态
     */
    @Column(name = "deleted")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 购物篮创建时间
     */
    @Column(name = "created_at")
    public Date createdAt;


    public static void updateCache(String key, Object value) {
        CacheHelper.objectToCache(key, value);
    }


    /**
     * 删除图片
     *
     * @param id
     */
    public static void deleteImg(long id) {
        Activity activity = Activity.findById(id);
        if (activity != null) {
            activity.mainImg = null;
            activity.save();
        }
    }

    public static Activity findByPosition(ShowPosition showPosition, long ahtId) {
        return Activity.find(" showPosition = ? and activityHomeTemplate.id = ? and deleted = ?", showPosition, ahtId, DeletedStatus.UN_DELETED).first();
    }

    public static List<Activity> findByPositions(ShowPosition showPosition, long ahtId) {
        return Activity.find(" showPosition = ? and activityHomeTemplate.id = ? and deleted = ? order by showOrder asc", showPosition, ahtId, DeletedStatus.UN_DELETED).fetch();
    }

    public static Activity findByNum(ShowPosition showPosition, int i) {
        return (Activity) Activity.find("showPosition = ? and deleted = ? order by showOrder asc ").fetch().get(i - 1);
    }

    public static List<Activity> findByTemplateWithDataBase(long id) {
        return Activity.find(" deleted = ? and activityHomeTemplate.id = ? ", DeletedStatus.UN_DELETED, id).fetch();
    }

    /**
     * 根据 商户使用的模板 查询 活动
     *
     * @param activityHomeTemplate
     * @return
     */
    public static List<Activity> findByHomeTemplate(ActivityHomeTemplate activityHomeTemplate) {
        return Activity.find("deleted = ? and activityHomeTemplate.id = ? order by showOrder asc", DeletedStatus.UN_DELETED, activityHomeTemplate.id).fetch();
//        return CacheHelper.getCache(CACHE_ACTIVITY_BY_TEMPID + activityHomeTemplate.id, new CacheCallBack<List<Activity>>() {
//            @Override
//            public List<Activity> loadData() {
//                return Activity.find("deleted = ? and activityHomeTemplate.id = ?" , DeletedStatus.UN_DELETED , activityHomeTemplate.id).fetch();
//            }
//        });
    }

    /**
     * 根据活动 拿到活动商品
     *
     * @return
     */
    public List<ActivityProduct> getActivityProducts() {
//        final Long _id = this.id;
        return ActivityProduct.find("deleted = ? and activity.id = ? order by isPC asc,id ", DeletedStatus.UN_DELETED, this.id).fetch();
//        return CacheHelper.getCache(ActivityProduct.CACHE_ACTIVITY_PRODUCT_BY_ACTIVITYID + this.id, new CacheCallBack<List<ActivityProduct>>() {
//            @Override
//            public List<ActivityProduct> loadData() {
//                return ActivityProduct.find("deleted = ? and activity.id = ?" , DeletedStatus.UN_DELETED , _id).fetch();
//            }
//        });
    }


    public static List<Activity> findByPositionAndTemplateId(ShowPosition showPosition, long ahtId) {
        return Activity.find(" showPosition = ? and activityHomeTemplate.id = ? and deleted = ? order by showOrder", showPosition, ahtId, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     *
     * @param id
     * @return
     */
    public static Activity fingByActivityId (long id){
        return Activity.find("id = ? and deleted = ?" , id , DeletedStatus.UN_DELETED).first();
    }


    /**
     * 查询模板某分类下的轮播图
     *
     * @param showPosition
     * @param classifyId
     * @return
     */
    public static Activity findByPostionAndClassify(ShowPosition showPosition , long classifyId ){
        return Activity.find(" showPosition = ?  and activityClassify.id = ? and  deleted = ?" , showPosition  , classifyId , DeletedStatus.UN_DELETED).first();
    }


    /**
     *
     * 查询模板某分类下活动
     *
     * @param showPosition
     * @param classifyId
     * @return
     */
    public static List<Activity> loadByPostionAndClassify(ShowPosition showPosition , long classifyId){
        return Activity.find(" showPosition = ?  and activityClassify.id = ? and  deleted = ?" , showPosition  , classifyId , DeletedStatus.UN_DELETED).fetch();

    }

    /**
     * 删除某分类下的所有活动,并将活动下的商品删除
     * @param classifyId
     */
    public static void deletedByClassifyId(long classifyId){
        String sqlUpdate = " update activity a  , activity_prmotion_activity_products b set a.deleted = 1 , b.deleted = 1 where a.activity_classify_id = "+ classifyId +" and b.activity_id = a.id";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }

    /**
     * 根据根据分类和模板查询活动
     * @param templateId
     * @param classifyId
     * @return
     */
    public static List<Activity> loadByTemplateAndClassify(long templateId , long classifyId){
        return Activity.find(" activityHomeTemplate.id = ? and activityClassify.id = ? and deleted = ?" , templateId , classifyId , DeletedStatus.UN_DELETED).fetch();
    }

}
